/**
 * Created by Miao on 2016/4/29.
 */
angular.module('weChat.controllers', [])
    .controller('messageController', function ($scope, $state, $ionicPopup, localStorageService, messageService) {

        // $scope.messages = messageService.getAllMessages();
        // console.log($scope.messages);
        $scope.onSwipeLeft = function () {
            /*$state.go("login");*/
        };
        $scope.popupMessageOpthins = function (index) {
            $scope.popup.index = index;
            $scope.popup.optionsPopup = $ionicPopup.show({
                templateUrl: "templates/popup.html",
                scope: $scope,
            });
            $scope.popup.isPopup = true;
        };
        $scope.markMessage = function () {
            var index = $scope.popup.index;
            var message = $scope.messages[index];
            if (message.showHints) {
                message.showHints = false;
                message.noReadMessages = 0;
            } else {
                message.showHints = true;
                message.noReadMessages = 1;
            }
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup = false;
            messageService.updateMessage(message);
        };
        $scope.deleteMessage = function () {
            var index = $scope.popup.index;
            var message = $scope.messages[index];
            $scope.messages.splice(index, 1);
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup = false;
            messageService.deleteMessageId(message.id);
            messageService.clearMessage(message);
        };
        $scope.topMessage = function () {
            var index = $scope.popup.index;
            var message = $scope.messages[index];
            if (message.isTop) {
                message.isTop = 0;
            } else {
                message.isTop = new Date().getTime();
            }
            $scope.popup.optionsPopup.close();
            $scope.popup.isPopup = false;
            messageService.updateMessage(message);
        };
        $scope.messageDetils = function (message) {
            $state.go("messageDetail", {
                "messageId": message.id
            });
        };
        $scope.$on("$ionicView.beforeEnter", function () {
            // console.log($scope.messages);
            $scope.messages = messageService.getAllMessages();
            $scope.popup = {
                isPopup: false,
                index: 0
            };
        });
    })
    .controller('userController', function ($scope, $ionicModal, $ionicPopover, $timeout, $location, $ionicPopup) {
    })
    .controller("loginController", function ($scope, $ionicPopup, $location) {
        $scope.login = function (user) {
            if (typeof(user) == 'undefined') {
                $ionicPopup.alert({
                    title: "Warning Message",
                    template: 'Please fill username and password to proceed'
                });
            }
            if (user.userName == '1' && user.password == '1') {
                $location.path('/tab/message');
            } else {
                $ionicPopup.alert({
                    title: "Warning Message",
                    template: 'Invalid username or password.'
                });
            }
        };
    });
